﻿using System;
using System.Collections.Generic;
using WebUI.BL.ModelView;
using WebUI.BL.User;
using System.Web.UI;
using WebUI.DAL;
using WebUI.Lang;
using System.Web;

namespace WebUI
{
    public partial class AdlerLogin : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
                DisplayLangOnControl();
        }

        private void DisplayLangOnControl()
        {
            btnLogin.Text = GetText("login.signin");
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string login = txtLoginId.Text.Trim();
            string pass = txtPassword.Text.Trim();
            try
            {
                string userrole = "";
                string groupno = "";
                var validUser = IsValidUserLogin(login, pass);
                if (validUser!=null)
                {
                    List<GroupModelView> gmv = getGroupModelView(login);
                    if (gmv != null)
                    {
                        foreach (var x in gmv)
                        {
                            if (userrole == "")
                                userrole += x.GroupName;
                            else
                                userrole += " - " + x.GroupName;

                            if (groupno == "")
                                groupno += x.GroupNo;
                            else
                                groupno += " - " + x.GroupNo;
                        }
                        
                    }

                    Response.Cookies.Get("adler_userid").Expires = DateTime.Now.AddDays(1);
                    Response.Cookies.Get("adler_userid").Value = validUser.UserLoginName;

                    Response.Cookies.Get("adler_userrole").Expires = DateTime.Now.AddDays(1);
                    Response.Cookies.Get("adler_userrole").Value = userrole;

                    Response.Cookies.Get("adler_groupno").Expires = DateTime.Now.AddDays(1);
                    Response.Cookies.Get("adler_groupno").Value = groupno;
                    Response.Redirect(Helpers.link.ToUserManager("UserManager.aspx"));
                    
                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, typeof(Page), "ShowValidation", "javascript:alert('Tên đăng nhập hoặc Mật khẩu không chính xác!');", true);
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }

        private List<GroupModelView> getGroupModelView(string login)
        {
            UserBL users = new UserBL();
            return users.getRole(login);
        }

        private tbl_User IsValidUserLogin(string login, string pass)
        {
            UserBL users = new UserBL();
             var user = users.checkLogin(login, pass);
            if (user!=null)
                return user; //thanh cong
            return null;
        }

        private void ChangeLanguage(string lang)
        {
            HttpCookie cookie = Request.Cookies["lang"];
            if (cookie != null)
            {
                Response.Cookies["lang"].Value = lang;
                Response.Redirect(Request.UrlReferrer.AbsoluteUri);
            }
        }

        protected void vi_Click(object sender, EventArgs e)
        {
            ChangeLanguage("vi");
        }

        protected void en_Click(object sender, EventArgs e)
        {
            ChangeLanguage("en");
        }

        
    }
}